Skip to content

Add union type - #766

Open
bvisness wants to merge 2 commits into
WebAssembly:mainfrom
bvisness:union
Open

bvisness wants to merge 2 commits into
WebAssembly:mainfrom
bvisness:union

Conversation

@bvisness

@bvisness bvisness commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Adds support for union (a variant of variant). The design of unions is mostly unchanged from what @eqrion originally proposed (maybe modulo some of the mapping from types to distinguishable kinds).

Resolves #724.

One thing I wanted to discuss is the distinguishability rules (which may eventually be reused for overloads). It's really hard to find a set of "distinguishable kinds" for each type that will actually avoid conflicts in various languages without being totally useless. The danger, in my opinion, is "kind creep", where you keep adding more distinguishable kinds to each component type, e.g. adding the list kind to the component map type because maps are frequently iterable in source languages. This reduces ambiguity for bindings, but makes unions much less useful.

In my opinion the right way to handle this is to be stricter about types when in a "distinguishable context". You can see this reflected in the updated JS API. A plain old list<string> parameter will accept any JS iterable (in TypeScript terms, Iterable<string>), whereas a union{ list<string>, ... } parameter requires a JS array (Array<string>). In other words, the runtime would literally run Array.isArray to decide if the value is "list-like", rather than checking for Symbol.iterator. This should only be a modest complexity cost for devs and one that TypeScript will catch right away. And importantly it ensures that union parameters should round-trip nicely; e.g. a union{ list, map } will flow out to JS as an Array or Map and back to components as a list or map, without any awkward crossover.

@bvisness
bvisness marked this pull request as ready for review November 24, 2026 17:29
@bvisness
bvisness requested a review from lukewagner November 24, 2026 17:29
Just a bunch of drive-by updates made as part of union/overload work.
@bvisness bvisness changed the title Add unions and overloads Add union type Nov 25, 2026
@bvisness

Copy link
Copy Markdown
Collaborator Author

I have updated this PR to only contain unions, and have moved overloads to #774.

@bvisness bvisness mentioned this pull request Nov 25, 2026

@lukewagner lukewagner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, looking good, nice work! I haven't looked at JS-Reference.md yet, but here's a few first comments and questions:

Comment thread design/mvp/Text.md
(result <valtype>? (error <valtype>)?) ↦ (variant (case "ok" <valtype>?) (case "error" <valtype>?))
string ↦ (list char)
(map <keytype> <valtype>) ↦ (list (tuple <keytype> <valtype>))
(union (case <labellit> <valtype>)+) ↦ (variant (case <labellit> <valtype>)+)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ultra-nit: can you put union after result (to put the variants together)

Comment thread design/mvp/Text.md
| '[method]' <label> '.' <label>
| '[method]' ('[get]' | '[set]') <label> '.' <label> 📡
| '[static]' <label> '.' <label>
| '[static]' ('[get]' | '[set]') <label> '.' <label> 📡

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the following refactoring and readability enhancements to the name descriptions and strong uniqueness are great, but could you put them into a separate PR (that we can promptly review and merge ahead of this PR).

Comment thread design/mvp/Text.md
languages to pick a case by inspecting the type of a source-language value.
Statically-typed languages can choose to represent `union`s like `variant`s
(with explicit case names) or as simple unions of types if their type system
permits (e.g. `string | number`).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
permits (e.g. `string | number`).
permits (e.g. `string | number` in TypeScript).

Comment thread design/mvp/Text.md
| `(option T)` | [...kinds of T, object] |
| `(own R)`, `(borrow R)` | [resource `R`] (types that are `eq` are indistinguishable) |
| `union` | kinds of all case payloads in declaration order, excluding duplicates |
| `stream`, `future`, `error-context` | *not allowed* |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if it's not implemented in the Web Preview, it seems like these 3 cases could be defined (emoji-gated), even if they're just object (b/c duck-typing).

Comment thread design/mvp/Text.md
</details>

Some types are not allowed to appear as a union case even if they have
distinguishability rules defined:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If they are rejected by validation, why do they have distinguishability rules defined? Why not just put them in a single "rejected" row?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! That's a holdover from overloads, which do perform distinguishability tests on those types. I should shuffle those over to the overloads PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anonymous union as a specialization of variant to support common web APIs

2 participants